package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.FinanceHandleModel;
import com.jiayang.hhotel.bean.SameListModel;
import com.jiayang.hhotel.fragment.AuditTabFragment;
import com.jiayang.hhotel.presenter.AuditPresenter;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.base.XFragmentAdapter;
import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/25 0025.
 * 查账页面
 */

public class AuditActivity extends XActivity<AuditPresenter> implements View.OnClickListener {

    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    private Button writeOffAccount;//冲销帐
    private Button transferAccounts;//转账

    private FinanceHandleModel.PCashierListDtosBean model;

    private List<Fragment> fragmentList= new ArrayList<>();
    private XFragmentAdapter adapter;

    @Override
    public void initData(Bundle savedInstanceState) {
        model = (FinanceHandleModel.PCashierListDtosBean) getIntent().getExtras().getSerializable(Constant.INTENT_PROJECT_KEY);

        initTitle(this,true,getString(R.string.audit),R.color.orange_color,null,null);

        mViewPager= (ViewPager) findViewById(R.id.viewPager);
        mTabLayout= (TabLayout) findViewById(R.id.tabLayout);
        writeOffAccount= (Button) findViewById(R.id.bt_write_off_account);
        transferAccounts= (Button) findViewById(R.id.bt_transfer_accounts);

        writeOffAccount.setOnClickListener(this);
        transferAccounts.setOnClickListener(this);

        fragmentList.clear();
        fragmentList.add(new AuditTabFragment(1,model));
        fragmentList.add(new AuditTabFragment(2,model));
        fragmentList.add(new AuditTabFragment(3,model));
        fragmentList.add(new AuditTabFragment(4,model));
        fragmentList.add(new AuditTabFragment(5,model));

        String[] titles=getResources().getStringArray(R.array.audit_list);

        if (adapter == null) {
            adapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
        }
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(5);

        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                LogUtil.showLog(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_audit;
    }

    @Override
    public AuditPresenter newP() {
        return new AuditPresenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_write_off_account://冲销帐
                getP().createLineRoomDialog(this,true);
                break;
            case R.id.bt_transfer_accounts://转账
                getP().createLineRoomDialog(this,false);
                break;
        }
    }

    /**
     * 调起保存请求
     * @param money 金额
     * @param trnCode 账项
     * @param comment 备注
     * @param is true：冲销帐；false：转账
     */
    public void saveAccount(double money,int trnCode,String comment,boolean is){
            getP().chongXiaoZhang(Constant.LAUGUAGE_FLAG_CHINESE,model.getGstAcct(),trnCode,money,comment);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==Constant.GUEST_ROOM_THING_REQUEST_CODE && resultCode==Constant.GUEST_ROOM_THING_RESULT_CODE){
            SameListModel.CceDtoListBean model= (SameListModel.CceDtoListBean) data.getExtras().getSerializable(Constant.INTENT_PROJECT_KEY);
            if(model != null){
                getP().getAccount().setText(model.getChnDescp());
                getP().getAccount().setTag(model.getCode());
            }
        }
    }
}
